$(function () {
	var lastIndex;
	$("#shipment").datagrid({
		fit:true, 
		nowrap:false, 
		striped:true, 
		collapsible:true, 
		sortOrder:"desc", 
		idField:"dictionaryID",
		url:js + "dict/viewDictionary.action?type=shipment", 
		fitColumns:true, height:"auto", 
		pagination:true, 
		singleSelect:true,
		columns:[[
			{field:"dictionaryCode", title:"编号", width:120, sortable:true, editor:"text"},
			{field:"dictionaryName", title:"名称", width:120, sortable:true, editor:"text"},
			{field:"note", title:"备注", width:120, sortable:true, editor:"text"}
		]], 
		rownumbers:true, 
		toolbar:[
			{text:"添加", iconCls:"icon-add", handler:function () {
				var row = $('#shipment').datagrid('getSelected');
				if (row){
					var index = $('#shipment').datagrid('getRowIndex', row);
					$("#shipment").datagrid("endEdit", index);
				}
				var url= js + "dict/insertDictionary.action?type=shipment";
				$.post(url, function(result){
					if(result.success){
						$("#shipment").datagrid("appendRow", 
						{dictionaryCode:"",dictionaryName:"",dictionaryID:result.id});
						lastIndex = $('#shipment').datagrid('getRows').length-1;
						$('#shipment').datagrid('selectRow', lastIndex);
						$('#shipment').datagrid('beginEdit', lastIndex);
					}
		 		},'json')
			}},'-',{
					text:'删除',
					iconCls:'icon-remove',
					handler:function(){
						var row = $('#shipment').datagrid('getSelected');
						if (row){
							var url= js + "dict/deleteDictionary.action?type=shipment&dictionaryID="+row.dictionaryID;
							$.post(url, function(result){
								if(result.success){
									var index = $('#shipment').datagrid('getRowIndex', row);
									$('#shipment').datagrid('deleteRow', index);
								}
					 		},'json')
						}
					}
				}
		], onBeforeLoad:function () {
			$(this).datagrid("rejectChanges");
		}, onClickRow:function (rowIndex) {
			if (lastIndex != rowIndex) {
				$("#shipment").datagrid("endEdit", lastIndex);
				$("#shipment").datagrid("beginEdit", rowIndex);
			}
			lastIndex = rowIndex;
		},
		onAfterEdit:function(rowIndex, rowData, changes){
			var hadChange=0;
			for(var name in changes){
				hadChange = 1 ;
				break;
			}
			if(hadChange){
			 	var url= js + "dict/updateDictionary.action?type=shipment";
				$.post(url,rowData, function(data){
		 		},'json')
			}
		}});
});

function lookup(value,name){
	var searchUrl;
	if('all'==name){
		value='';	
		searchUrl = js+'dict/viewDictionary.action?type=shipment&searchInput='+value;
	}else if('dictionaryCode'==name){
		searchUrl = js+'dict/viewDictionary.action?type=shipment&searchInput='+value+"&searchKind=dictionaryCode";
	}else if('dictionaryName'==name){
		searchUrl = js+'dict/viewDictionary.action?type=shipment&searchInput='+value+"&searchKind=dictionaryName";
	}
	$('#shipment').datagrid({
		 url:searchUrl,
    });
}

